<script setup lang="ts">
import { onMounted } from "vue";
import useCategoryStore from "@/store/modules/category.ts";

let categoryStore = useCategoryStore();

onMounted(() => {
  getC1();
});

// 获取一级分类
const getC1 = async () => {
  await categoryStore.getC1();
};

// 一级分类改变的回调函数：获取二级分类列表
const handler = async () => {
  await categoryStore.getC2();
  categoryStore.c2Id = "";
  categoryStore.c3Id = "";
  categoryStore.c3Arr = [];
};

// 二级分类改变的回调函数：获取三级分类列表
const handler1 = async () => {
  await categoryStore.getC3();
  categoryStore.c3Id = "";
};

// 接收父组件传递的场景参数  对于指定场景，禁用下拉框
defineProps(["scene"]);

defineOptions({
  name: "Category",
});
</script>

<template>
  <el-form :inline="true">
    <el-form-item label="一级分类">
      <el-select
        :disabled="!(scene == 0)"
        v-model="categoryStore.c1Id"
        @change="handler"
      >
        <el-option
          v-for="c1 in categoryStore.c1Arr"
          :label="c1.name"
          :key="c1.id"
          :value="c1.id"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="二级分类">
      <el-select
        :disabled="!(scene == 0)"
        v-model="categoryStore.c2Id"
        @change="handler1"
      >
        <el-option
          v-for="c2 in categoryStore.c2Arr"
          :label="c2.name"
          :key="c2.id"
          :value="c2.id"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="三级分类">
      <el-select :disabled="!(scene == 0)" v-model="categoryStore.c3Id">
        <el-option
          v-for="c3 in categoryStore.c3Arr"
          :label="c3.name"
          :key="c3.id"
          :value="c3.id"
        ></el-option>
      </el-select>
    </el-form-item>
  </el-form>
</template>

<style scoped lang="scss"></style>
